package com.lookoutthebush.companyindex.services.impl;

import com.lookoutthebush.companyindex.dao.NewsTypesDAO;
import com.lookoutthebush.companyindex.domain.NewsTypesDO;
import com.lookoutthebush.companyindex.enums.NewsDeleteEnum;
import com.lookoutthebush.companyindex.exception.BizException;
import com.lookoutthebush.companyindex.services.INewsTypeService;
import com.lookoutthebush.companyindex.util.CheckUtil;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 咨询类型
 *
 * @author LookOutTheBush
 * @date 2021/9/17 8:33 下午
 */
public class NewsTypeServiceImpl implements INewsTypeService {

    @Resource
    private NewsTypesDAO newsTypesDAO;

    /**
     * 创建
     *
     * @param newsTypesDO 实体
     * @return 主键id
     */
    @Override
    public Long createNewsType(NewsTypesDO newsTypesDO) {
        String field = CheckUtil.isEmpty(newsTypesDO, "userId", "type", "desc");
        if (!ObjectUtils.isEmpty(field)) {
            throw new BizException(field + "can not be null");
        }
        return newsTypesDAO.insert(newsTypesDO);
    }

    /**
     * 查询所有有效的（未删除的）类型
     *
     * @return list
     */
    @Override
    public List<NewsTypesDO> getAllValidTypes() {
        return newsTypesDAO.queryAllValidTypes();
    }

    /**
     * 根据主键删除
     * @param id 主键
     * @return boolean
     */
    @Override
    public boolean deleteById(Long id) {
        NewsTypesDO newsTypesDO = newsTypesDAO.queryById(id);
        if(newsTypesDO == null){
            return true;
        }
        newsTypesDO.setDelete(NewsDeleteEnum.YES.getValue());
        newsTypesDAO.update(newsTypesDO);
        return true;
    }
}
